﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DamageReceiverCylinderCollider : DamageReceiverCollider {
    #region Fields

    public Transform A;
    public Transform B;

    public float radius;

    #endregion Fields

    #region Methods
    void OnStart() {
        init();
    }

    public override void CheckInsideColliderRay() {

        Collider[] s1 = Physics.OverlapSphere(A.position, radius, layersToHit.value);
        Collider[] s2 = Physics.OverlapSphere(B.position, radius, layersToHit.value);
        AddColliers(s1);
        AddColliers(s2);
    }


    //public Transform testDummy;
    public override bool PointInsideCollider(Vector3 aPoint) {
        aPoint.y = 0;
        Vector3 APos = A.position.SetY(0f);
        Vector3 BPos = B.position.SetY(0f);
        Vector3 projectionOnAB = Vector3.Project(aPoint - APos, BPos - APos) + APos;

        float ABSqrMagnitude = (APos - BPos).sqrMagnitude;
        float sqrRadius = radius * radius;
        return

            (aPoint - APos).sqrMagnitude <= sqrRadius ||
            (aPoint - BPos).sqrMagnitude <= sqrRadius ||

            (projectionOnAB - APos).sqrMagnitude <= ABSqrMagnitude &&
            (projectionOnAB - BPos).sqrMagnitude <= ABSqrMagnitude &&
            (aPoint - projectionOnAB).sqrMagnitude <= radius * radius;
    }

    void OnDrawGizmos() {
        if(DamageReceiverCollider.IsDrawColliderWire == false) {
            return;
        }
        if(enabled) {
            //Gizmos.color = Color.red;
            //Gizmos.DrawLine(A.position, B.position);

            Gizmos.color = Color.green;
            ////Gizmos.DrawLine(testDummy.position, Vector3.Project(testDummy.position - A.position, B.position - A.position) + A.position);

            Gizmos.DrawWireSphere(A.position, radius);
            Gizmos.DrawWireSphere(B.position, radius);
            Gizmos.DrawLine(A.position, B.position);
        }
    }

    #endregion Methods
}